﻿using CPPEI.Net.TCP;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Net
{
    public class NetServerData
    {
        /// <summary>
        /// 在线的客户端数量
        /// </summary>
        public int ClientCount { get; private set; }
        public NetClientProxy[] ClientList { get; private set; }
        public NetRoomData[] RoomArray { get; private set; }

        public NetServerData()
        {
            ClientList = new NetClientProxy[1000];

            RoomArray = new NetRoomData[100];
        }

        #region client
        public void AddClient(NetClientProxy client)
        {
            for (uint index = 0; index < ClientList.Length; index++)
            {
                if (ClientList[index] == null)
                {
                    ClientList[index] = client;
                    client.Id = index;
                    ClientCount++;
                    return;
                }
            }
        }

        public void RemoveClient(NetClientProxy client)
        {
            if (ClientList[client.Id] == client)
            {
                ClientList[client.Id] = null;
                ClientCount--;
            }
        }

        public NetClientProxy GetClient(uint clientID)
        {
            return ClientList[clientID];
        }
        #endregion

        #region room
        public void AddRoom(NetRoomData roomData)
        {
            for (uint index = 0; index < RoomArray.Length; index++)
            {
                if (RoomArray[index] == null)
                {
                    RoomArray[index] = roomData;
                    roomData.ID = index;
                    return;
                }
            }
        }

        public void RemoveRoom(NetRoomData roomData)
        {
            roomData.RemoveAllClient();
            RoomArray[roomData.ID] = null;
        }

        public NetRoomData GetRoom(uint roomID)
        {
            if (roomID >= RoomArray.Length)
            {
                return null;
            }
            return RoomArray[roomID];
        }
        #endregion

        #region api
        public void SendToClient(NetClientProxy clientProxy, byte[] data)
        {
            clientProxy.SendData(data);
        }
        #endregion
    }
}
